/*
 * Copyright the original author or authors.
 * 
 * Licensed under the MOZILLA PUBLIC LICENSE, Version 1.1 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.mozilla.org/MPL/MPL-1.1.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import pl.milib.data.info.MIEventInfo;
import pl.milib.managers.MouseManager;
import pl.milib.mc.abstract.AbstractButton;
import pl.milib.mc.abstract.AbstractButtonView;
import pl.milib.mc.buttonViews.ButtonViewContrast;
import pl.milib.mc.buttonViews.ButtonViewFourFrames;
import pl.milib.mc.buttonViews.ButtonViewIniMidEnd;
import pl.milib.mc.buttonViews.ButtonViewPlayToEnd;
import pl.milib.mc.service.MIMC;
import pl.milib.util.MILibUtil;

/**
 * @author Marek Brun 'minim'
 */
class pl.milib.mc.service.MIButton extends AbstractButton {
	
	public var btn : Button;
	private var mm : MouseManager;
	private var isDragEventsEnabled : Boolean;
	
	private function MIButton(btn:Button) {
		this.btn=btn;
		btn.onPress=MILibUtil.createDelegate(this, setPress);
		btn.onRelease=MILibUtil.createDelegate(this, setRelease);
		btn.onReleaseOutside=MILibUtil.createDelegate(this, setReleaseOutside);
		btn.onRollOut=MILibUtil.createDelegate(this, setRollOut);
		btn.onRollOver=MILibUtil.createDelegate(this, setRollOver);
		btn.onDragOut=MILibUtil.createDelegate(this, setDragOut);
		btn.onDragOver=MILibUtil.createDelegate(this, setDragOver);		btn.onSetFocus=MILibUtil.createDelegate(this, setRollOver);		btn.onKillFocus=MILibUtil.createDelegate(this, setRollOut);		btn['onUnload']=MILibUtil.createDelegate(this, onBtnUnload); //...because it can be MovieClip
		MIMC.forInstance(btn._parent).getDeleter().addDeletingSub(this);
		isEnabled=btn.enabled;
		mm=MouseManager.getInstance();
	}//<>
	
	public function setIsDragEventsEnabled(bool:Boolean):Void {
		isDragEventsEnabled=bool;
		if(bool){
			btn.trackAsMenu=true;
		}else{
			btn.trackAsMenu=false;
		}
	}//<<
	
	public function setIsEnabled(bool:Boolean):Void {
		bool=Boolean(bool);
		if(bool==btn.enabled){ return; }
		isEnabled=bool;
		if(!isEnabled){ doArtificialOut(); }		btn.enabled=isEnabled;
		bev(event_EnabledChanged);
	}//<<
	
	public function getIsEnabled(Void):Boolean {
		return btn.enabled;
	}//<<
	
	private function setIsOver(bool):Void {
		isOver=bool;
	}//<<
	
	private function setPress(Void):Void {
		if(isDragEventsEnabled){ mm.addListener(this); }
		super.setPress();
	}//<<	private function setReleaseOutside(Void):Void {
		if(isDragEventsEnabled){ mm.removeListener(this); }
		super.setReleaseOutside();
	}//<<	private function setRollOut(Void):Void {
		if(isDragEventsEnabled){ mm.removeListener(this); }
		super.setRollOut();
	}//<<
	
	public function getServicedObject(Void):Button {
		return btn;
	}//<<
	
	private function doDelete(Void):Void {
		if(btn){
			var unmc:MovieClip=btn._parent.createEmptyMovieClip('buttonOverwriter', btn.getDepth());
			unmc.unloadMovie();
		}
		super.doDelete();
	}//<<
	
	public function setupAutoView(Void):Void {
		if(!gotViews() && btn instanceof MovieClip){
			var btnMC:MovieClip=MovieClip(btn);
			switch(btnMC._totalframes){
				case 1:
					addButtonView(new ButtonViewContrast(btnMC));
				break;
				case 3:
					addButtonView(new ButtonViewFourFrames(btnMC));
				break;
				case 4:
					addButtonView(new ButtonViewFourFrames(btnMC));
				break;
				default:
					btnMC.gotoAndStop(1);
					btnMC.gotoAndStop('end');
					if(MovieClip(btn)._currentframe>3){
						addButtonView(new ButtonViewIniMidEnd(btnMC));
					}else{
						addButtonView(new ButtonViewPlayToEnd(btnMC));
					}
			}
		}
	}//<<
	
	static public function gotForInstance(btn:Button):Boolean {
		return MILibUtil.getObjectMILibObject(btn).serviceByMIButton!=null;
	}//<<
	
	static public function forInstance(btn/*Button or MovieClip*/):MIButton {
		var milibObjObj:Object=MILibUtil.getObjectMILibObject(btn);
		if(!milibObjObj.serviceByMIButton){ milibObjObj.serviceByMIButton=new MIButton(btn); }
		return milibObjObj.serviceByMIButton;
	}//<<
	
	public function setOverAndDisableAndFreezeViews(Void):Void {
		setRollOver();
		var viewsArr:Array=views.getArray();
		for(var i=0,view:AbstractButtonView;i<viewsArr.length;i++){
			view=viewsArr[i];
			view.setIsFreez(true);
		}
		btn.enabled=false;
	}//<<
	
	public function unsetOverAndDisableAndFreezeViews(Void):Void {
		btn.enabled=true;
		var viewsArr:Array=views.getArray();
		for(var i=0,view:AbstractButtonView;i<viewsArr.length;i++){
			view=viewsArr[i];
			view.setIsFreez(false);
		}
		setRelease();
		setRollOut();
	}//<<
	
//****************************************************************************
// EVENTS for MIButton
//****************************************************************************
	private function onEvent(ev:MIEventInfo) {
		//super.onEvent(ev);
		switch(ev.hero){
			case mm:
				switch(ev.event){
					case mm.event_Up:
						if(isPressed){
							setReleaseOutside();
						}
					break;
				}
			break;
		}
	}//<<Events
	
	private function onBtnUnload(Void):Void {
		getDeleter().DELETE();
	}//<<
	
}